﻿<%@ WebHandler Language="C#" Class="OfferPriceHandler" %>

using System;
using System.Web;
using System.Collections;
using System.Data;
using StarTech.DBUtility;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Web.DynamicData;


public class OfferPriceHandler : IHttpHandler
{
    AdoHelper ado = AdoHelper.CreateHelper(StarTech.Util.AppConfig.DBInstance);
    string memberId = "";
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (context.Request.Cookies["memberInfo"] != null && Common.CheckUser(context.Request.Cookies["memberInfo"].Value))
        {
            memberId = context.Request.Cookies["memberInfo"].Value.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)[0];
            string flag = Common.NullToEmpty(context.Request["flag"]);

            string phone = Common.NullToEmpty(context.Request["phone"]);
            string trueName = Common.NullToEmpty(context.Request["trueName"]);
            string address = Common.NullToEmpty(context.Request["address"]);
            string userId = Common.NullToEmpty(context.Request["userId"]);

            switch (flag.ToLower())
            {
                case "stars":
                    context.Response.Write(GetUserStars(userId));
                    break;
                case "info":
                    context.Response.Write(GetUserInfo());
                    break;
                case "fill":
                    context.Response.Write(FillUserInfo(trueName, phone, address));
                    break;
                case "upload":
                    if (context.Request.Files.Count == 0)
                    {
                        context.Response.Write("请上传文件");  //代表文件为空
                    }
                    else
                    {
                        string info = uploadPic(context.Request.Files[0]);
                        context.Response.Write(info);  //代表上传成功
                    }
                    break;
            }
        }
    }
    
    private string GetUserStars(string userId)
    {
        string strSql = @"select AVG(convert(int,commentType)) as star,count(1) as num from T_Order_Comment where memberid=@userid";
        SqlParameter[] p = new SqlParameter[] {
            new SqlParameter("@userid",userId)
        };
        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
    }

    private string GetUserInfo()
	{
        string strSql = @"select MemberId,TrueName,Mobile,AddressDetail,HeadImg,LevelName from T_Member_Info where MemberId=@MemberId";
        SqlParameter[] p = new SqlParameter[] {
            new SqlParameter("@MemberId",memberId)
        };
        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql,p).Tables[0]);
	}

    private string FillUserInfo(string trueName,string phone, string address)
    {
        string strSql = @"update T_Member_Info set trueName=@trueName,mobile=@mobile,AddressDetail=@AddressDetail where MemberId=@MemberId";
        SqlParameter[] p = new SqlParameter[] {
            new SqlParameter("@MemberId",memberId),
            new SqlParameter("@trueName",trueName),
            new SqlParameter("@mobile",phone),
            new SqlParameter("@AddressDetail",address),
        };
        return ado.ExecuteSqlNonQuery(strSql,p).ToString();
    }

    private string uploadPic(HttpPostedFile uploadFile)
    {
        string fileTail = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf('.')).ToLower();
        string savePath = "/upload/headimg/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MMdd") + "/";
        if (!Directory.Exists(HttpContext.Current.Server.MapPath(savePath)))
            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(savePath));
        savePath += DateTime.Now.ToString("HHmmssffff") + fileTail;
        uploadFile.SaveAs(HttpContext.Current.Server.MapPath(savePath));

        string strSql = "update t_member_info set headimg=@path where memberid=@memberid;";
        SqlParameter[] p = new SqlParameter[] {
            new SqlParameter("@memberid",memberId),
            new SqlParameter("@path",savePath),
        };
        ado.ExecuteSqlNonQuery(strSql, p);
        return "1_$$_" + savePath;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}